package com.chuangke.mds.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.chuangke.file.model.FileInfo;
import com.chuangke.mds.service.MdsSimulateReportQuarterService;
import com.chuangke.mds.service.TechnologyProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.http.HttpResult;
import com.chuangke.mds.entity.TechnologyProcessProcedureNodeSimulate;
import com.chuangke.mds.service.TechnologyProcessProcedureNodeSimulateService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("zchkcl/processSimulate")
public class TechnologyProcessProcedureNodeSimulateController {

	@Autowired
	private TechnologyProcessProcedureNodeSimulateService simulateService;
	@Autowired
	private TechnologyProcessService technologyProcessService;
	@Autowired
	private MdsSimulateReportQuarterService mdsSimulateReportQuarterService;

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/save")
	public HttpResult save(@RequestBody TechnologyProcessProcedureNodeSimulate record) {
		return HttpResult.ok(simulateService.saveOrUpdate(record));
	}


	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/delete")
	public HttpResult delete(@RequestBody List<String> billIds) {
		simulateService.delete(billIds);
		return HttpResult.ok();
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/page")
	public HttpResult page(@RequestBody Map<String, Object> params) {
		return HttpResult.ok(simulateService.page(new CkPageQuery(params)));
	}
	

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/load")
	public HttpResult load(@RequestParam String id) {
		return HttpResult.ok(simulateService.load(id));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/loadForOrigin")
	public HttpResult loadForOrigin(@RequestParam String id) {
		return HttpResult.ok(simulateService.loadForOrigin(id));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/loadByProcedure")
	public HttpResult loadByProcedure(@RequestParam Map<String, Object> params) {
		return HttpResult.ok(simulateService.loadInfoByProcedure(params));
	}


	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/loadByProcess")
	public HttpResult loadByProcess(@RequestParam Map<String, Object> params) {
		return HttpResult.ok(simulateService.loadByNode(new CkPageQuery(params)));
	}
	@PostMapping(value ="/transferResultFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public ResponseEntity receiveSimulationResultFile(@RequestPart(value ="resultFiles", required = false) List<MultipartFile> resultFiles) {
		try {
			List< FileInfo> fileInfos = new ArrayList<>();
			for (MultipartFile file : resultFiles){
				FileInfo fileInfo =technologyProcessService.upload(file);
				fileInfos.add(fileInfo);
			}
			return ResponseEntity.ok().body(fileInfos);
		} catch (Exception e) {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
					.body("上传失败: " + e.getMessage());
		}
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/loadByBatchIds")
	public HttpResult loadByBatchIds(@RequestBody List<String> billIds) {
		return HttpResult.ok(simulateService.loadByBatchId(billIds));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/updateInfoByPython")
	public HttpResult updateStatus(@RequestBody TechnologyProcessProcedureNodeSimulate model) {
		return HttpResult.ok(simulateService.updateInfoByPython(model));
	}
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/getPythonLog")
	public HttpResult getPythonLog(@RequestBody Map<String, Object> params) {
		String id = (String) params.get("id");
		return HttpResult.ok(simulateService.getPythonLog(id));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/exportSimulate")
	public void exportSimulate(@RequestParam String id, HttpServletResponse response) throws IOException {
		mdsSimulateReportQuarterService.exportSimulate(id,response);
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/getHomePageData")
	public HttpResult getHomePageData(@RequestBody Map<String, Object> params) {
		return HttpResult.ok(simulateService.getHomePageData());
	}

}
